<?php
                                                                                    
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('fmfrmvoucher_loadGrid');      
$objXajax->registerFunction('fmfrmvoucher_save');
$objXajax->registerFunction('fmfrmvoucher_edit'); 

function fmfrmvoucher_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix,$hotel_id;
    $objResponse =  new xajaxResponse();
    $name = $params['txtname'];
    $type = $params['type'];
    $description = addslashes(strip_tags($params['txtdescription']));
    $percent = $params['txtpercent'];    
    $type = $params['type'];    
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        $sql ="UPDATE fm_promotion SET name = '$name',description = '$description',type = '$type',
            percent = '$percent' WHERE id =$id"; 
        $objDbSelect->Execute($sql);
        
    }else{
          $sql= " INSERT INTO fm_promotion(name,percent,description,service_id,active,type,flg_promotion) VALUES('$name','$percent','$description',$hotel_id,1,$type,0)"; 
          $objDbSelect->Execute($sql); 
     }    
     if($objDbSelect->Execute($sql)){
       $objResponse->addScript("checkUpdateStatus(1,'fmfrmvoucher','$edit');");
       $objResponse->addScript('xajax_fmfrmvoucher_loadGrid();'); 
        }else{
       $objResponse->addScript("checkUpdateStatus(0,'fmfrmvoucher','$edit');");
    }
    $objResponse->addScript("hasChange= true;");
/*    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addScript('xajax_fmfrmvoucher_loadGrid();');*/     
    return $objResponse->getXML();
}


function fmfrmvoucher_loadGrid()
{ 
    global $lang,$path;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = fmfrmvoucher_getGrid(); 
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/fm_voucher_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmvoucher_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('fmfrmvoucher_tbl');");     
    $objResponse->addScript('changeWaiting(1);');                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function fmfrmvoucher_getGrid()
{
    global $objDbSelect,$lang,$hotel_id;
    $sqlSelect = " select * from fm_promotion where service_id =  $hotel_id and flg_promotion=0";                                                     
    return $objDbSelect->GetArray($sqlSelect);
}  

    function fmfrmvoucher_edit($id){
        global $objDbSelect,$tbl_postfix;
        $objResponse =  new xajaxResponse();
        $sql = " SELECT * FROM fm_promotion WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name       = $arr[0]['name']; 
        $percent    = $arr[0]['percent'];
        $type    = $arr[0]['type'];
        
        $description =htmlspecialchars_decode($arr[0]['description']) ;
        $objResponse->addAssign('fmfrmvoucher_txtdescription','value',$description); 
        
        $objResponse->addScript("$('#fmfrmvoucher_txtname').val('$name');");
        $objResponse->addScript("$('#fmfrmvoucher_txtpercent').val('$percent');");
        $objResponse->addScript("$('#fmfrmvoucher_type$type').attr('checked','checked');"); 
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
?>
